<template>
	<view class="page">
		<u-navbar title="授权登录" :placeholder="true" :titleStyle="titleStyle" leftIconColor="#fff" :bgColor="bgColor"
			:autoBack="true">
			<!-- <view class="u-nav-slot" slot="right"><span class="registerText" @click="toPage('/pages/register/index')">注册</span></view> -->
		</u-navbar>


		<view class="flexLogin" style="margin-top: 80rpx;">
			<view class="loginBtn main-bg-color">授权手机号<button class="getPhoneBtn" open-type="getPhoneNumber"
					@getphonenumber="getPhoneNumber"></button></view>



			<view class="loginBtn main-bg-color" @click="login">直接登录</view>
		</view>


	</view>
</template>

<script>
	export default {
		data() {
			return {
				bgColor: '#08C060',
				titleStyle: {
					color: '#fff'
				},
				form: {
					code: '',
					openId: '',
					otherAvatarUrl: '',
					otherNickname: '',
					otherPhone: '',
					otherSex: '',
					otherUsername: '',
					provider: 'weibo',
				}
			};
		},
		onLoad(e) {
			this.form.code = e.code
			this.form.openId = e.openId
			this.form.otherAvatarUrl = e.avatarUrl
			this.form.otherNickname = e.nickName
			this.form.otherSex = e.gender
			this.form.otherUsername = e.nickName
		},
		methods: {
			getPhoneNumber(e) {
				uni.login({
					success: loginRes => {
						let data = {
							code: loginRes.code,
							encryptedData: e.detail.encryptedData,
							iv: e.detail.iv,
						}
						this.$api.getWxmpPhoneNo(data).then(res => {
							let res2 = JSON.parse(res)
							this.form.otherPhone = res2.phoneNumber
							this.login()
						})
					}
				})

			},
			login() {
				this.$api.loginloginByWp2(this.form).then(res => {
					uni.setStorageSync('token', res.token)
					uni.setStorageSync('familyId', res.familyIds)
					uni.setStorageSync('nickname', res.nickname)
					uni.setStorageSync('username', res.username)
					uni.setStorageSync('showPayment', res.showPayment)
					uni.setStorageSync('userNewCalendar', res.userNewCalendar)

					uni.setStorageSync('maxImageMegBytes', res.maxImageMegBytes)
					uni.setStorageSync('maxVideoMegBytes', res.maxVideoMegBytes)
					uni.setStorageSync('systemUser', res.systemUser)



					let menberdata = uni.getStorageSync('menberdata') ? uni.getStorageSync('menberdata') : ''
					uni.hideLoading()
					if (menberdata) {
						console.log(menberdata)

						this.$api.acceptInvite(menberdata).then(res => {
							this.$Show.show('家庭加入成功')
							uni.removeStorageSync('menberdata')
							setTimeout(() => {
								uni.reLaunch({
									url: '/pages/tabbar/index/index'
								})
							}, 1500)
						})
					} else {
						console.log(res)
						uni.reLaunch({
							url: '/pages/tabbar/index/index'
						})
					}

				})
			},
		}
	};
</script>

<style lang="scss">
	.loginBtn {
		width: 690rpx;
		height: 80rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		color: #fff;
		margin: 0 auto;
		margin-top: 40rpx;
		border-radius: 10rpx;
		box-sizing: border-box;
		position: relative;

		.getPhoneBtn {
			position: absolute;
			left: 0;
			right: 0;
			top: 0;
			bottom: 0;
			z-index: 10;
			opacity: 0;
		}
	}
</style>